import { loadEnv } from 'vite'
import { resolve, join } from 'path'
import type { UserConfig, ConfigEnv } from 'vite'
import { pluginsConfig, proxyConfig } from './build'
export default ({ command, mode }: ConfigEnv): UserConfig => {
  const env = loadEnv(mode, process.cwd())
  const { VITE_APP_PORT } = env
  console.log(command) // serve build 可根据执行命令执行不同的配置文件 如更换serve中的proxy 本项目不做处理
  return {
    define: {
      'process.env': {}
    },
    plugins: pluginsConfig,
    server: {
      open: true,
      host: true,
      port: Number(VITE_APP_PORT),
      hmr: {
        overlay: false
      },
      proxy: proxyConfig(env, command)
    },
    css: {
      preprocessorOptions: {
        scss: {
          additionalData: '@import "src/style/variables.scss";',
          javascriptEnabled: true
        }
      }
    },
    esbuild: {
      drop: command === 'build' ? ['debugger'] : []
    },
    // 'console',
    resolve: {
      alias: [
        {
          find: /\@\//,
          replacement: join(__dirname, 'src/')
        }
      ]
    },
    optimizeDeps: {
      include: ['vue', 'vue-router', 'pinia', 'vue-types', 'axios']
    },
    build: {
      sourcemap: false,
      chunkSizeWarningLimit: 4000,
      rollupOptions: {
        input: {
          index: resolve(__dirname, '.', 'index.html')
        },
        output: {
          chunkFileNames: 'static/js/[name]-[hash].js',
          entryFileNames: 'static/js/[name]-[hash].js',
          assetFileNames: 'static/[ext]/[name]-[hash].[ext]'
        }
      }
    }
  }
}
